<?php
namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\console\input\Argument;

class Console extends Command
{
	protected $namespace = "app\\command\\controller\\";
	protected $classController = "";
	protected $className = "";
	protected $action = "";
	protected $arg = array();
	 
    protected function configure()
    {
        $this->setName('console')->setDescription('Here is the remark ');
        $this->addArgument('name', Argument::REQUIRED, "the controller and action");
        $this->addArgument('arg', Argument::IS_ARRAY, "The function arg");
    }

    protected function execute(Input $input, Output $output)
    {
    	$name = trim($input->getArgument('name'));
    	if(empty($name)){
    		$output->writeln("命令模式为controller/action");
    		exit();
    	}
    	$options  = explode('/',$name);
    	if(count($options) < 2){
    		$output->writeln("命令模式为controller/action");
    		exit();
    	}
    	$this->className = $this->namespace . ucfirst($options[0]);
    	$this->action = $options[1];
    	if(!class_exists($this->className)){
    		$output->writeln("类{$this->className}不存在");
    		exit();
    	}
    	
    	$this->classController = new $this->className;
    	
    	if(!method_exists($this->classController,$this->action) ){
    		$output->writeln("{$this->className}不存在{$this->action}");
    		exit();
    	}
    	return  call_user_func_array(array($this->classController, $this->action), $input->getArgument('arg'));
    }
    
}